<?php

namespace Orders;

/**
 * 工具类
 */
class Utils
{

    public static function curlRequest($url, array $publicData, array $headerData, array $bodyData)
    {


        return self::arrayToxml($publicData, $headerData, $bodyData);
    }

    private static function arrayToxml(array $publicData, array $headerData, array $bodyData)
    {
        $shopList = '';
        foreach ($bodyData as $key => $value) {
            $num = $key+1;
            $shopList .= " 
    <OrderList>
        <gnum>{$num}</gnum>
        <itemRecordNo>{$value['itemRecordNo']}</itemRecordNo>
        <itemNo>{$value['itemNo']}</itemNo>
        <itemName>{$value['itemName']}</itemName>
        <itemDescribe>{$value['itemDescribe']}</itemDescribe>
        <barCode>{$value['barCode']}</barCode>
        <gcode>{$value['gcode']}</gcode>
        <brand>{$value['brand']}</brand>
        <gmodel>{$value['gmodel']}</gmodel>
        <qty>{$value['qty']}</qty>
        <unitCus>{$value['unitCus']}</unitCus>
        <unitCiq>{$value['unitCiq']}</unitCiq>
        <qty1>{$value['qty1']}</qty1>
        <unitCus1>{$value['unitCus1']}</unitCus1>
        <price>{$value['price']}</price>
        <totalPrice>{$value['totalPrice']}</totalPrice>
        <currencyCus>{$value['currencyCus']}</currencyCus>
        <currencyCiq>{$value['currencyCiq']}</currencyCiq>
        <wrapTypeCus></wrapTypeCus>
        <wrapTypeCiq></wrapTypeCiq>
        <country>{$value['country']}</country>
        <usage>{$value['usage']}</usage>
        <note></note>
        <field1></field1>
        <field2></field2>
        <field3></field3>
    </OrderList>";
        }

        //用法
        $rsa=new RSA();
        $xmlString = "<?xml version='1.0' encoding='utf-8'?>
<Order>
    <OrderHead>
        <appType>{$headerData['appType']}</appType>
        <orderType>{$headerData['orderType']}</orderType>
        <appStatus>{$headerData['appStatus']}</appStatus>
        <orderNo>{$headerData['orderNo']}</orderNo>
        <acturalPaid>{$headerData['acturalPaid']}</acturalPaid>
        <goodsValue>{$headerData['goodsValue']}</goodsValue>
        <freight>{$headerData['freight']}</freight>
        <discount>{$headerData['discount']}</discount>
        <taxTotal>{$headerData['taxTotal']}</taxTotal>
        <currencyCus>{$headerData['currencyCus']}</currencyCus>
        <currencyCiq>{$headerData['currencyCiq']}</currencyCiq>
        <shipperCountry>{$headerData['shipperCountry']}</shipperCountry>
        <shipper>{$headerData['shipper']}</shipper>
        <shipperAddress>{$headerData['shipperAddress']}</shipperAddress>
        <shipperTelephone>{$headerData['shipperTelephone']}</shipperTelephone>
        <buyerRegNo>{$headerData['buyerRegNo']}</buyerRegNo>
        <buyerName>{$headerData['buyerName']}</buyerName>
        <buyerIdType>{$headerData['buyerIdType']}</buyerIdType>
        <buyerIdNumber>{$headerData['buyerIdNumber']}</buyerIdNumber>
        <buyerTelephone>{$headerData['buyerTelephone']}</buyerTelephone>
        <consignee>{$headerData['consignee']}</consignee>
        <eProvince>{$headerData['eProvince']}</eProvince>
        <consigneeCity>{$headerData['consigneeCity']}</consigneeCity>
        <consigneeZone>{$headerData['consigneeZone']}</consigneeZone>
        <consigneeAddress>{$headerData['consigneeAddress']}</consigneeAddress>
        <consigneeDistrict>{$headerData['consigneeDistrict']}</consigneeDistrict>
        <consigneeTelephone>{$headerData['consigneeTelephone']}</consigneeTelephone>
        <consigneeIdType>{$headerData['consigneeIdType']}</consigneeIdType>
        <consigneeIdNumber>{$headerData['consigneeIdNumber']}</consigneeIdNumber>
        <consigneeCountry>{$headerData['consigneeCountry']}</consigneeCountry>
        <logisticsCode>{$headerData['logisticsCode']}</logisticsCode>
        <logisticsNo>{$headerData['logisticsNo']}</logisticsNo>
        <payCode>{$headerData['payCode']}</payCode>
        <payTransactionId>{$headerData['payTransactionId']}</payTransactionId>
        <batchNumbers>{$headerData['batchNumbers']}</batchNumbers>
        <assureCode>{$headerData['assureCode']}</assureCode>
        <emsNo>{$headerData['emsNo']}</emsNo>
        <declTime>{$headerData['declTime']}</declTime>
        <ieDate></ieDate>
        <agentCodeCus>{$headerData['agentCodeCus']}</agentCodeCus>
        <agentCodeCiq>{$headerData['agentCodeCiq']}</agentCodeCiq>
        <areaCode>{$headerData['areaCode']}</areaCode>
        <tradeMode>{$headerData['tradeMode']}</tradeMode>
        <trafMode>{$headerData['trafMode']}</trafMode>
        <trafNo>{$headerData['trafNo']}</trafNo>
        <voyageNo></voyageNo>
        <billNo></billNo>
        <loctNo></loctNo>
        <licenseNo></licenseNo>
        <country>{$headerData['country']}</country>
        <insuredFee>{$headerData['insuredFee']}</insuredFee>
        <packageTypeCus>{$headerData['packageTypeCus']}</packageTypeCus>
        <packageTypeCiq>{$headerData['packageTypeCiq']}</packageTypeCiq>
        <packNo>{$headerData['packNo']}</packNo>
        <grossWeight>{$headerData['grossWeight']}</grossWeight>
        <netWeight>{$headerData['netWeight']}</netWeight>
        <note></note>
        <headField1></headField1>
        <headField2></headField2>
        <headField3></headField3>
        <headField4></headField4>
        <headField5></headField5>
        <headField6></headField6>
    </OrderHead>
    {$shopList}
</Order>";
 

        //加密
        $DataInfo = $rsa->encrypt($xmlString);
        $publicData['Signature'] = md5($DataInfo);

        $data = self::setPublic($publicData,$DataInfo);
        $data = $rsa->http_post('http://emy.hnblc.com:9955/etouch/service/pushOrder',['xml'=>$data]);
          file_put_contents('log7.txt',var_export($data, true));
        $xml = simplexml_load_string($data);
        
        // 将SimpleXML对象转换成JSON
        $json = json_decode(json_encode($xml),true);
        return $json;
// echo '<pre>';
// print_r($json);

    }
    private static function setPublic($publicData,$DataInfo)
    {
        // 创建一个 XML 文档
        $xml = new \DOMDocument('1.0', 'UTF-8');

        // 添加根元素
        $root = $xml->createElement('Root');
        $xml->appendChild($root);

        self::arrayToXmlHelper($root, $xml, 'PubInfo', $publicData);
// 创建一个文本节点
        $dataInfoTextNode = $xml->createTextNode($DataInfo);
        
        // 创建 DataInfo 元素
        $dataInfoElement = $xml->createElement('DataInfo');
        
        // 将文本节点添加为 DataInfo 元素的子节点
        $dataInfoElement->appendChild($dataInfoTextNode);
        
        // 将 DataInfo 元素添加到根元素下
        $root->appendChild($dataInfoElement);

        return $xml->saveXML();;
    }

    private static function arrayToXmlHelper($root, $xml, $element_name, $param)
    {
        // 添加子元素和文本内容
        $child = $xml->createElement($element_name);
        $root->appendChild($child);

        foreach ($param as $key => $value) {
            $version = $xml->createElement($key, $value);
            $child->appendChild($version);
        }
        return $xml;
    }



}
